function search(p){
  $("#divLoading").show();
  $("#list").hide();
  var isNew = $('#isNew').val();
  var status = parseInt($('#status').val());
  if(isNew == '0' && status > 1){
	 alert('旧数据无"非未审核状态记录"，审核的旧数据直接合并到新数据');
	 return false;
  }
  
  var data = {
    type_id: $("#selTypeOne").val(),
    type_id_value: $("#selTypeTwo").val(),
    ltitle: $("#ltitle").val(),
	status: $("#status").val(),
	isNew: $("#isNew").val(),
    page:p?p:0
  };
  ajax.timeout=0;
  ajax.send(url+"/search",data,function(obj){
    var list = obj.data.list;
    var page = obj.data.page;
    var size = obj.data.size;
    var count = obj.data.count;
    _page = page;
    replaceHtml("list", _getHTMLList(obj.data,page,size));
	setTable();
    $(".divPage").html(__foot.html(page,size,count,search));
    $("#divLoading").hide();
	$("#list").show();
  });
}

function _getHTMLList(data,page,size){
 var list=data.list;	
  dids = [];
  if (list == "") return "<div style='width:95%;margin:5px auto;text-align:center;border:1px #ccc solid;border-style:solid none solid none;color:blue'>无相关记录</div>";
  var str = [], p = 0;
  str[p++] = '<form action="/main/admin.php/lawAdmin/edit" id="f1"><table border="1" align="center" cellpadding="0" cellspacing="1">';
  str[p++] = '<tr><td></td><td nowrap>序号</td><td>ID</td><td>标题</td><td nowrap>行业</td><!--<td nowrap>专业</td>--><td nowrap>地区</td><td>类型</td><td nowrap>应用</td><td nowrap>颁布单位/部门</td><td nowrap>发布时间</td><td nowrap>生效时间</td><td nowrap>失效时间</td><td nowrap>操作</td></tr>';
  str[p++] = '<tr><td colspan="13" style="text-align:right"><input type="button" onclick="return subForm(0)" value="提交修改"></td></tr>';
  var hyArr=data.hy;
  var zyArr=data.zy_select;
  var ltypeArr=data.type;
  var regionArr=data.region;
  var appArr=data.appType;
  
  for (var i in list) {
    var index = (page*1-1)*size+i*1+1;
    var l = list[i];
    dids.push(l.id);
	
	//专业分类
	/*var zyStr="<select class='zyids' name='zyids[]' id='zy_"+l.id+"'><option value=\"0\">-----</option>";
	for(var j in zyArr){
		//alert(zyArr.length);
		var zy=zyArr[j];
		var kids=zy.kids;
		zyStr+="<optgroup label='"+zy.title+"'>";
		//alert(kids.length);
		for(var z in kids){
		  var kid=kids[z]	
		  zyStr+='<option value="'+kid.id+'"';
		 if(kid.id==l.zy_id)
		   zyStr+='selected="selected"';
		   zyStr+='>'+kid.title+'</option>';
		}
		zyStr+="</optgroup>";
	}
	zyStr+='</select>';*/
	//lert(zyStr);
	
	//行业分类
	var hyStr="<select class='hyids' name='hyids[]' id='hy_"+l.id+"'><option value=\"0\">-----</option>";
	for(var j in hyArr){
		//alert(zyArr.length);
		var hy=hyArr[j];
		var kids=hy.kids;
		hyStr+="<optgroup label='"+hy.title+"'>";
		//alert(kids.length);
		for(var z in kids){
		  var kid=kids[z]	
		  hyStr+='<option value="'+kid.id+'"';
		 if(kid.id==l.hy_id)
		   hyStr+='selected="selected"';
		   hyStr+='>'+kid.title+'</option>';
		}
		hyStr+="</optgroup>";
	}
	hyStr+='</select>';
	
	//法律 分类
	var ltypeStr="<select class='typeids' name='typeids[]' id='type_"+l.id+"'><option value=\"0\">-----</option>";
	for(var j in ltypeArr){
		var ltype=ltypeArr[j];
		  ltypeStr+='<option value="'+ltype.id+'"';
		if(ltype.id==l.lawtype_id)
		  ltypeStr+='selected="selected"';
		ltypeStr+=">"+ltype.name+"</option>";
	}
	ltypeStr+='</select>';
	
	
	//地区分类
	var regionStr="<select class='regionids' name='regionids[]' id='dq_"+l.id+"'><option value=\"0\">-----</option>";
	//alert(regionArr.length);
	for(var j in regionArr){
		var reg=regionArr[j];
		var kids=reg.kids;
		regionStr+="<optgroup label='"+reg.parent.title+"'>";
		if(reg.kids.length>1) {
		  regionStr+="<option value='"+reg.parent.id+"'";
		  if(l.dq_id==reg.parent.id)
		    regionStr+='selected="selected"';
		  regionStr+='>'+reg.parent.title+'(全)</option>';
		}
		for(var k in kids) {
			var kid=kids[k];
			regionStr+='<option value="'+kid.id+'"';
			if(kid.id==l.dq_id)
		    regionStr+='selected="selected"';
			regionStr+='>'+kid.title+'</option>';
		}
		regionStr+=reg.parent.title+"</optgroup>";
	}
	regionStr+='</select>';
	//alert(regionStr);
	//应用分类
	var appStr="<select class='appids' name='appids[]' id='app_"+l.id+"'><option value=\"0\">-----</option>";
	for(var j in appArr){
		var app=appArr[j];
	    appStr+='<option value="'+app.id+'"';
		if(app.id==l.app_type)
		  	appStr+='selected="selected"';
		appStr+=">"+app.name+"</option>";
	}
	appStr+='</select>';
	
	var color_id='c0';
	if(l.status==2)
	  color_id='c1';
	else {
		if(l.status>=3)
         color_id='c2';
	}
	//alert(color_id);
    str[p++] = '<tbody><tr id="tr_'+l.id+'"><td><input type="checkbox" onclick="checkRecord(this)" record_id='+l.id+' /></td>';
    str[p++] = '<td>'+index+'</td><td class="lawid" id="id_'+l.id+'">'+l.id+'</td><td id="title_'+l.id+'"><a class="'+color_id+'" id="a_'+l.id+'" href="/law-detail.php?id='+l.id+'" target="_blank">'+l.title+'</a></td>';
    //str[p++] = '<td>'+hyStr+'</td><td>'+zyStr+'</td><td>'+regionStr+'</td><td>'+ltypeStr+'</td><td>'+appStr+'</td>';   含专业栏
	str[p++] = '<td>'+hyStr+'</td><td>'+regionStr+'</td><td>'+ltypeStr+'</td><td>'+appStr+'</td>';    //不含专业栏
    str[p++] = '<td class="cc" id="luser_'+l.id+'" >'+l.issue+'</td><td><input name="date_pub" id="pub_'+l.id+'" class="Wdate" type="text" size="10" maxlength="10" value="'+l.date_pub+'" onClick="WdatePicker()"/></td>';
	str[p++] = '<td><input name="date_job" id="job_'+l.id+'" class="Wdate" type="text" size="10" maxlength="10" value="'+l.ldate_job+'" onClick="WdatePicker()"/></td>';
	str[p++] = '<td><input name="date_stop" id="stop_'+l.id+'" class="Wdate" type="text" size="10" maxlength="10" value="'+l.ldate_stop+'" onClick="WdatePicker()"/></td>';
    str[p++] = '<td><a href="#" onclick="return setUser('+l.id+')">部门</a><br><a href="javascript:void(0)" onclick="subForm('+l.id+')">提交</a><br><a href="/main/admin.php?s=/LawAdmin/insert/id/'+l.id+'" target="_blank">编辑</a><!--<br><a href="javascript:void(0)" onclick="deleteRecord('+l.id+')">删除</a><br>--></td>';
    str[p++] = '</tr>';
  }
  str[p++] = '</tbody><tr ><td colspan="11"><div class="divPage"></div></td><td colspan="2"><input type="button" onclick="return subForm(0)" value="提交修改"></td><tr></table></form>';
  return str.join('');
}

 function setTable(){ 
    $("tbody tr:odd").css("background-color","#eee"); 
    var numId = $("tbody td.cc"); 
    numId.click(function(){ 
        var tdIns = $(this); 
        if ( tdIns.children("input").length>0 ){ return false; }        

        var inputIns = $("<input type=’text’/>"); //需要插入的输入框代码 
        var text = $(this).html();         

        inputIns.width(tdIns.width()); //设置input与td宽度一致 
        inputIns.val(tdIns.html()); //将本来单元格td内容copy到插入的文本框input中 
        tdIns.html(""); //删除原来单元格td内容 
        inputIns.appendTo(tdIns).focus().select(); //将需要插入的输入框代码插入dom节点中 
        inputIns.click(function(){ return false;}); 
        //处理Enter和Esc事件 
        inputIns.keyup(function(event){ 
                var keycode = event.which; 
                if( keycode == 13 ){ var inputText = $(this).val(); tdIns.html(inputText);} 
                if( keycode == 27 ){ tdIns.html(text);} 
        }); 
    }); 

}

function checkRecord(self){
  var id = $(self).attr("record_id");
  if ($(self).attr("checked")){
    lids.push(id);
  }else {
    for (var i = lids.length - 1; i >= 0 ; i--) {
      if (lids[i] == id) lids.splice(i,1);
    }
  }
}

//1-全选，2-不选，3-反选
function clickLnkSelect(self,type) {
  if (type == 1) {
    $(self).parent().parent().find('input[type=checkbox]').attr("checked",true);
    lids = dids;
  }else if (type == 2){
    $(self).parent().parent().find('input[type=checkbox]').attr("checked",false);
    lids = [];
  }else {
    $(self).parent().parent().find('input[type=checkbox]').each(function(i,self){
      if ($(self).attr('checked')) $(self).attr('checked',false);
      else $(self).attr('checked',true);
      checkRecord(self);
    });
  }
}

function deleteRecord(id){
 if(confirm('将执行删除该记录，删除后无法找回，是否继续')){	
  var data = {
    id:id
  };
  ajax.send(url+"/ajaxDeleteRecord",data,function(obj){
    search(_page);
  });
 }
}

function changeSelType(self){
  if ($(self).val() == "") {
    $("#selTypeTwo").html('<option value="">全部</option>');
    $("#selFunction").html('<option value="">全部</option>');
    return ;
  }
  var data = {
    type:$(self).val()
  };
  ajax.send(url+'/ajaxChangeSelType',data,function(obj){
    var list = obj.data.list;
    var type = obj.data.type_id;
    $("#selTypeTwo").html(_getHTMLSelTypeTwo(list,type));
    $("#selFunction").html(_getHTMLSelTypeTwo(list,type));
  });
}

function _getHTMLSelTypeTwo(list,type){
  var str = "<option value=\"0\">-------</option>";
  if (type == "lawtype_id" || type == 'app') {
    for (var i in list) {
      var l = list[i];
      str += '<option value="'+l.id+'">'+l.title+'</option>';
    }
  }else {
    for (var i in list) {
      var l = list[i];
      var lids = [l.id];
      var option = '';
      for (var j in l.downLine) {
        var d = l.downLine[j];
        option += '<option value="'+d.id+'">'+d.title+'</option>';
        lids.push(d.id);
      }
      str += '<option value="'+lids.join(',')+'" style="color:red">'+l.title+'</option>';
      str += option;
    }
  }
  return str;
}

function deleteRecords(){
  if (lids.length == 0) {
    alert("请选择记录");
    return ;
  }
  var id = lids.join(',');
  var data = {
    id:id
  };
  ajax.send(url+"/ajaxDeleteRecord",data,function(obj){
    search(0);
  });
}

function changeTypeRecords(){
  if (lids.length == 0) {
    alert("请选择记录");
    return ;
  }
  if ($("#selFunction").val() == "") {
    alert("没有选择类型");
    return ;
  }
  var data = {
    type_id: $("#selFunction").val(),
    id: lids.join(','),
    type: $("#selTypeOne").val()
  };
  $('#list').hide();
  $('#divLoading').show();
  ajax.send(url+"/ajaxChangeTypeRecords",data,function(obj){
	//alert(data.status);												   
    if(obj.status==1)search(0);//alert(obj.info);
	else alert(obj.info);
	//$('#list').show();
    //$('#divLoading').hide();
  });
}

//提交审核
function shenhe(){
  if (lids.length == 0) {
    alert("请选择记录");
    return ;
  }
  if($('#status').val()!=null && $('#status').val()!=''){
  	var id = lids.join(',');
	var isNew = $('#isNew').val();
	var status = $('#status').val();
	if(status == '3'){
	 alert('二审完成，无需再审核');  
      return false;
	}
	if(isNew == '1')
	  status = parseInt(status) +1;
  	var data = {
    	id:id,
		status:status
  	};
 	 ajax.send(url+"/ajaxShenhe",data,function(obj){
    if(obj.status==1){
		 alert(obj.info);
		for(var i in lids){
		  var lid=lids[i];
		  /*var c=parseInt($('#status').val());
		  var cla='c'+c;
		  //alert(cla);
		  $('#a_'+lid).removeClass();*/
		  $('#tr_'+lid).hide();
		}
	}
		else alert(obj.info);
  	});
  	}else{
	  alert('请先选择记录的状态');
  }
}

function subForm(id){
  /*ajax.send(url+"/editAjax",data,function(obj){
    search(0);
  });*/
  
  
  	var luser=new Array();
  	/*var zy_id=new Array();*/
  	var hy_id=new Array();
  	var dq_id=new Array();
	var app_id=new Array();
  	var ids=new Array();
	var pub=new Array();
	var job=new Array();
	var stop2=new Array();
  	var lawtype_id=new Array();
  if(id=='0'){
   $(".lawid").each(function(){  
	   ids.push($(this).html());
	 // alert($(this).html());
	  //data{ltitle:a}
    });
  
  // var numId = $("tbody td.cc"); 
/*   $("tbody td.cc").each(function(){ 
        var tdIns = $(this); 
        if ( tdIns.children("input").length>0 ){
			.children("input").
			}
	 });*/
  
  $(".cc").each(function(){  
	  var tdIns = $(this); 
        if ( tdIns.children("input").length>0 ){
		  var obj=tdIns.children("input");	
		  luser.push(obj.val());
		}else{
		  luser.push(tdIns.html());			
		}
	 // alert($(this).html());
	  //data{luser:a}
  });
  
 /* $(".zyids").each(function(){  
	  zy_id.push($(this).val());
	  //data{luser:a}
  });*/
  
  $(".hyids").each(function(){  
	  hy_id.push($(this).val());
	  //data{luser:a}
  });
  
  $(".typeids").each(function(){  
	  lawtype_id.push($(this).val());
	  //data{luser:a}
  });
  
  $(".regionids").each(function(){  
	  dq_id.push($(this).val());
	  //data{luser:a}
  });
  
  $(".appids").each(function(){  
	  app_id.push($(this).val());
	  //data{luser:a}
  });
  
  $("[name=date_pub]").each(function(){  
	  pub.push($(this).val());
	  //data{luser:a}
  });
  
  
  $("[name=date_job]").each(function(){  
	  job.push($(this).val());
	  //data{luser:a}
  });
  
  $("[name=date_stop]").each(function(){  
	  stop2.push($(this).val());
	  //data{luser:a}
  });
  
  }else{//修改单条记录
	 ids.push($('#id_'+id).html()); 
	/* zy_id.push($('#zy_'+id).val());*/
	 hy_id.push($('#hy_'+id).val());
	 dq_id.push($('#dq_'+id).val());
	 job.push($('#job_'+id).val());
	 pub.push($('#pub_'+id).val());
	 stop2.push($('#stop_'+id).val());
	 app_id.push($('#app_'+id).val());
	 if ( $('#luser_'+id).children("input").length>0 ){
		  var obj=$('#luser_'+id).children("input");	
		  luser.push(obj.val());
		}else{
		  luser.push($('#luser_'+id).html());			
		}
	 lawtype_id.push($('#type_'+id).val());
	//  alert(id);
  }
  
  var data={'ids':ids,
            /*'zy_id':zy_id,*/
            'hy_id':hy_id,
			'dq_id':dq_id,
			'lawtype_id':lawtype_id,
			'app_type':app_id,
			'luser':luser,
			'ldate_pub':pub,
			'ldate_job':job,
			'ldate_stop':stop2
			}
  $('#list').hide();
  $('#divLoading').show();
  ajax.send(url+'/editAjax',
			data,
			function(data){
			  if(data.data=='1'){alert('更新完成');}//search(_page);
			  else alert('更新出错');
			$('#list').show();
			$('#divLoading').hide();
		});
  
}



    
